<template>
	<view class="container">
		<view class="orderReceiving" v-if="showOrderReceiving">
			<view class="title">订单接受中······</view>
		</view>
		<view class="order-container" v-else>
			
			<view class="order-list" v-for="(item ,index) of orderList" :key="index" @tap="goOrderDetail(item.orderId)">
				<view class="market-title">佳佳超市</view>
				<view class="order-info">
					<view class="order-id">订单编号：<text>{{ item.orderId }}</text></view>
					<view class="order-wrap1">
						<view class="price">金额：<text>{{ item.orderAmount }}</text> 元</view>
						<view>状态：<button type="warn">{{ compileStatus(item.orderState)}}</button></view>
					</view>
					<view class="order-wrap2">
						<view class="username">{{ item.userName }}</view>
						<view class="time">时间：<text>{{ initTime(item.createTime) }}</text></view>
					</view>
				</view>
			</view>
			
			
		</view>
		<view class="botNav">查看历史订单</view>
	</view>

</template>

<script>
	import {mapGetters} from 'vuex' //引入mapGetters
	
	export default {
		data() {
			return {
				showOrderReceiving: true,
				orderList: [],
				userId: ''
			};
		},
		onShow(){
			this.getUserId();
		},
		computed: {
			...mapGetters(['getUserName'])
		},
		methods: {
			getUserId(){
				uni.$http({
					url: `/sys/user/getUserId?userName=${this.getUserName}`,
					method: "GET",
				}).then((res) => {
					
					const _data = res[1].data;
					
					if(_data.msg !== 'success'){
						uni.showToast({
							title: '验证码不能为空',
							icon: 'none'
						});
					}else{
						this.userId = _data.data;
						this.buildLinkWebSocket();
					}
				})
			},
			buildLinkWebSocket(){
				const _this = this;
				
				uni.connectSocket({
					url: `ws://47.98.146.179:8080/renren-admin/notice/${ this.userId }`
					// url: `ws://47.98.146.179:8080/renren-admin/notice/1625688966233128962`
				});
				
				uni.onSocketOpen(function (res) {
				  console.log('WebSocket连接已打开！');
				});
				
				uni.onSocketMessage(function(data){
					
					const _data = JSON.parse(data.data);
					if(_data.noticeType == 'old'){
						_this.orderList = _data.noticeInfo;
						_this.showOrderReceiving = false;
					}
					if(_data.noticeType == 'new'){
						
					}
				})
			},
			goOrderDetail(orderId){
				if(orderId){
					uni.navigateTo({
						url: `/shopReception/pages/orderDetail/orderDetail?orderId=${orderId}`,
						events: {
							refreshData: function(){
							}
						}
					})
				};
			},
			compileStatus(str){
				if(str == '0'){
					return '未分拣'
				}else if(str == '1'){
					return '分拣中···'
				}else if(str == '2'){
					return '吧台打包'
				}else if(str == '3'){
					return '打包完成'
				}
			},
			initTime(time){
				const date = new Date(time);
				
				const year = date.getFullYear();
				const month = date.getMonth() + 1;
				const day = date.getDate();
				const hour = date.getHours();
				const minute = date.getMinutes();
				const second = date.getSeconds();
				
				return `${year}-${month}-${day} ${hour}:${minute}:${second}`
			}
		},
		onHide(){
			uni.closeSocket({
				success:function(res){
				}
			});
		}
	}
</script>

<style lang="scss">
	@keyframes animated-border {
		0%{
			box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.4);
		}
		100%{
			box-shadow: 0 0 0 200rpx rgba(255, 255, 255, 0);
		}
	}
	
	page{
		width: 100%;
		height: 100%;
		background-color: aquamarine;

		
		.container{
			width: 100%;
			height: 100%;
		}
	}

	.orderReceiving{
		display: flex;
		width: 100%;
		height: 100%;
		justify-content: center;
		margin-top: 200rpx;
		
		.title{
			width: 250rpx;
			height: 250rpx;
			line-height: 250rpx;
			text-align: center;
			font-family: Arial, Helvetica, sans-serif;
			color: black;
			border: 6rpx solid green;
			padding: 15rpx;
			border-radius: 50%;
			animation: animated-border 3s infinite;
		}
	}

	.botNav{
		position: fixed;
		bottom: 0rpx;
		right: 0rpx;
		height: 80rpx;
		width: 220rpx;
		line-height: 80rpx;
		text-align: center;
		font-family: Arial, Helvetica, sans-serif;
		background-color: ghostwhite;
		border-radius: 8rpx;
	}
	
	.order-container{
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		height: 100%;
		background-color: aquamarine;
		
		.order-list{
			width: 700rpx;
			height: 300rpx;
			margin-top: 30rpx;
			box-shadow: 4rpx 8rpx 16rpx #666;
			background-color: #fff;
			border-radius: 20rpx;
			
			.market-title{
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				border-bottom: 3rpx solid #000;
				margin-left: 40rpx;
				margin-right: 40rpx;
			}
			
			.order-info{
				margin:20rpx 40rpx 0 40rpx;
				width: 100%;
				
				.order-wrap1{
					display: flex;
					height: 73rpx;
					
					view{
						width: 50%;
						height: 73rpx;
						line-height: 73rpx;
						
						text{
							color: red;
							font-weight: bold;
						}
						
						button{
							display: inline-block;
							position: relative;
							top: 8rpx;
							height: 50rpx;
							width: 150rpx;
							font-size: 20rpx;
							line-height: 40rpx;
							margin: 0;
							padding: 0;
						}
					}
				}
				
				.order-wrap2{
					display: flex;
					height: 73rpx;
					
					.username{
						width: 30%;
						height: 73rpx;
						line-height: 73rpx;
					}
					
					.time{
						width: 70%;
						height: 73rpx;
						line-height: 73rpx;
						font-size: 35rpx;
						
						text{
							color: #0000FF;
						}
					}
				}
			}
		}
	}

</style>
